package com.example.manager.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.manager.entity.CourseSelection;
import com.example.manager.entity.ScoreComposition;
import com.example.manager.entity.StudentSubScore;
import com.example.manager.repository.CourseSelectionRepository;
import com.example.manager.repository.ScoreCompositionRepository;
import com.example.manager.repository.StudentSubScoreRepository;
@Service
@Transactional
public class StudentSubScoreServiceImpl implements StudentSubScoreService {

	@Autowired
	private StudentSubScoreRepository ssRepo;
	@Autowired
	private CourseSelectionRepository csRepo;
	@Autowired
	private ScoreCompositionRepository scRepo;
	
	@Override
	public List<StudentSubScore> getByCourseSelection(CourseSelection cs) {
		return ssRepo.getByCourseSelection(cs);
	}

	@Override
	public List<StudentSubScore> getByScoreComposition(ScoreComposition sc) {
		return ssRepo.getByScoreComposition(sc);
	}

	@Override
	public List<StudentSubScore> findAll() {
		
		return ssRepo.findAll();
	}

	@Override
	public List<StudentSubScore> getByCourseSelection(Integer id) {
		return ssRepo.getByCourseSelection(csRepo.getOne(id));
	}

	@Override
	public List<StudentSubScore> getByScoreComposition(Integer id) {
		return ssRepo.getByScoreComposition(scRepo.getOne(id));
	}

	@Override
	public String postStudentSubScore(Double subScore, Integer courseSelectionId, Integer scoreCompositionId) {
		CourseSelection cs = csRepo.getOne(courseSelectionId);
		ScoreComposition sc = scRepo.getOne(scoreCompositionId);
		StudentSubScore ss = new StudentSubScore(cs, sc, subScore);
		ssRepo.save(ss);
		return "success";
	}


	@Override
	public String putStuSubScore(Double subScore, Integer courseSelectionId, Integer scoreCompositionId) {

		CourseSelection cs = csRepo.getOne(courseSelectionId);
		ScoreComposition sc = scRepo.getOne(scoreCompositionId);
		StudentSubScore ss = new StudentSubScore(cs, sc, subScore);
		ssRepo.save(ss);
		return "success";

	}

	@Override
	public String delStuSubScore(Integer id) {
		ssRepo.deleteById(id);
		return "success";
	}

	@Override
	public String postStudentSubScore(List<StudentSubScore> scores) {
		ssRepo.saveAll(scores);
		return "success";
	}

	@Override
	public String putStuSubScore(StudentSubScore ss) {
		Integer id = ss.getId();
		Double subScore = ss.getSubScore();
		CourseSelection cs = ss.getCourseSelection();
		ScoreComposition sc = ss.getScoreComposition();
		StudentSubScore ss2 = ssRepo.getOne(id);
		ss2.setSubScore(subScore);
		ss2.setCourseSelection(cs);
		ss2.setScoreComposition(sc);
		ssRepo.save(ss2);
		
		
		return "success";
	}

}
